// miniprogram/pages/deviceInfo/detailInfo/index.js
import Dialog from '@vant/weapp/dialog/dialog';
const app =  getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    AliasName:'',
    deviceInfo:{},
    show:false,
    currentVersion:'', //设备当前版本
    dstVersion:''  //可升级版本
  },
  

  //获取上一页传递的数据
  getInfo:function(){
    let that = this;
    const eventChannel = that.getOpenerEventChannel();
    eventChannel.on('getDeviceInfo',function(data){
      that.setData({
        deviceInfo:data.data.deviceinfo
      });
      console.log(that.data.deviceInfo);
    })
  },

  deleteBtn:function(){
    this.setData({ show:true });
  },

    //删除设备
  deleteDevice:function(){
    let that = this;
    app.sdk.init().then(()=>{
      app.sdk.requestApi('AppDeleteDeviceInFamily',{
        FamilyId:'default',
        DeviceName:that.data.deviceInfo.DeviceName,
        ProductId:that.data.deviceInfo.ProductId
      })
      .then((res)=>{
        console.log(res);
        wx.showToast({
          title: '删除成功',
          icon:'success',
          duration: 1500,
          mask: true,
          success:()=>{
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        })
      })
    })
  },

  goChangeName:function(){
    let that = this;
    wx.navigateTo({
      url: `/pages/deviceInfo/changeName/index?AliasName=${ this.data.AliasName }`,
      success:(result)=>{
        result.eventChannel.emit('getDeviceInfo',{ data:that.data.deviceInfo });
      }
    })
  },

  goInfoList:function(e){
    wx.navigateTo({
      url: '/pages/deviceInfo/infoList/index',
      success:(res)=>{
        res.eventChannel.emit('getDeviceInfo',{ data:e.currentTarget.dataset });
      }
    })
  },

  //检查设备升级
  checkUpdate:function(){
    let that = this;
    // let dialogData = {
    //   message: '正在检查设备版本···',
    //   theme:'round-button'
    // };
    // Dialog.alert(dialogData);
    wx.showLoading({
      title: '正在检查设备版本···',
      mask:true
    })
    app.sdk.init().then(()=>{
      app.sdk.requestApi('AppCheckFirmwareUpdate',{
        ProductId:that.data.deviceInfo.ProductId,
        DeviceName:that.data.deviceInfo.DeviceName
      })
      .then((res)=>{
        if(res.DstVersion === ''){
          // dialogData['message'] = '当前已是最新版本';
          // Dialog.alert(dialogData);
          wx.hideLoading({
            success: () => {
              wx.showToast({
                title: '当前已是最新版本',
              })
            },
          })
        }
        that.setData({
          currentVersion:res.CurrentVersion,
          dstVersion:res.DstVersion
        })
      })
      .catch((err)=>{
        // dialogData['message'] = '检查失败';
        // Dialog.alert(dialogData);
        wx.hideLoading({
          success: () => {
            wx.showToast({
              title: '检查失败',
              icon:'error'
            })
          },
        })
        console.log(err);
      })
    })
  },

  goShareDevice:function(){
    wx.showToast({
      title: '暂不支持该功能',
      icon:'error'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ AliasName:options.AliasName });
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})